package com.example.utils;

import com.example.service.impl.IpfsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SplitFileUtil {

//    private static final int splitSize = 1024*1024*2;


    private SplitFileUtil(){

    }


    public static ArrayList<File> splitFile(File file, int size) throws IOException {
        String source = "/opt/demo/back/assets/";
        int index = file.getName().lastIndexOf(".");
        //源文件的后缀名
        String extName = file.getName().substring(index);
        //拆分后的子文件列表
        ArrayList<File> fileList = new ArrayList<>();

        try {
            FileInputStream fis = new FileInputStream(file);


            byte[] bytes = new byte[size*1024*1024];
            int len = 0;
            int order = 1;
            while((len = fis.read(bytes)) != -1){
                System.out.println(order);
                File blockFile = new File(source + "//" +  order + extName);
                FileOutputStream fos = new FileOutputStream(blockFile);
                fos.write(bytes, 0, len);
                fos.close();
                order++;

                fileList.add(blockFile);

            }

            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fileList;

    }
}
